﻿using System;
using System.IO;
using System.Net.Sockets;

using Utilities.IO;
using System.Net;

namespace Utilities.Net
{
    public class TcpConnection : Connection
    {
        public TcpConnection(TcpClient tcpClient) : this(tcpClient, BitOrder.LittleEndian, typeof(sbyte))
        {
        }
        public TcpConnection(TcpClient tcpClient, BitOrder bitOrder) : this(tcpClient, bitOrder, typeof(sbyte))
        {
        }
        public TcpConnection(TcpClient tcpClient, Type stringPrefixType) : this(tcpClient, BitOrder.LittleEndian, stringPrefixType)
        {
        }
        public TcpConnection(TcpClient tcpClient, BitOrder bitOrder, Type stringPrefixType) : base(bitOrder, stringPrefixType)
        {
            client = tcpClient;
            stream = client.GetStream();
            reader = new BinaryReader(stream);
            writer = new BinaryWriter(stream);
        }

        public TcpConnection(string hostname, int port) : this(hostname, port, BitOrder.LittleEndian, typeof(sbyte))
        {
        }
        public TcpConnection(string hostname, int port, BitOrder bitOrder) : this(hostname, port, bitOrder, typeof(sbyte))
        {
        }
        public TcpConnection(string hostname, int port, Type stringPrefixType) : this(hostname, port, BitOrder.LittleEndian, stringPrefixType)
        {
        }
        public TcpConnection(string hostname, int port, BitOrder bitOrder, Type stringPrefixType) : base(bitOrder, stringPrefixType)
        {
            client = new TcpClient(hostname, port);
            stream = client.GetStream();
            reader = new BinaryReader(stream);
            writer = new BinaryWriter(stream);
        }

        public TcpConnection(IPAddress address, int port) : this(address, port, BitOrder.LittleEndian, typeof(sbyte))
        {
        }
        public TcpConnection(IPAddress address, int port, BitOrder bitOrder) : this(address, port, bitOrder, typeof(sbyte))
        {
        }
        public TcpConnection(IPAddress address, int port, Type stringPrefixType) : this(address, port, BitOrder.LittleEndian, stringPrefixType)
        {
        }
        public TcpConnection(IPAddress address, int port, BitOrder bitOrder, Type stringPrefixType) : base(bitOrder, stringPrefixType)
        {
            client = new TcpClient(new IPEndPoint(address, port));
            stream = client.GetStream();
            reader = new BinaryReader(stream);
            writer = new BinaryWriter(stream);
        }
    }
}
